package io.renren.modules.dictionary.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.ConstantUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.dictionary.entity.DictionaryCustomEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.dictionary.entity.DictionaryEntity;
import io.renren.modules.dictionary.service.DictionaryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author 杨帅
 * @email 1252732174@qq.com
 * @date 2018-10-17 14:15:28
 */
@RestController
@RequestMapping("/dictionary/dictionary")
public class DictionaryController {
	@Autowired
	private DictionaryService dictionaryService;
	
	/**
	 * 列表
	 */
	@SysLog("查询数据字典列表")
	@RequestMapping("/list")
	@RequiresPermissions("dictionary:dictionary:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<DictionaryEntity> dictionaryList = dictionaryService.queryList(query);
		int total = dictionaryService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dictionaryList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@SysLog("查询数据字典信息")
	@RequestMapping("/info/{dictionaryid}")
	@RequiresPermissions("dictionary:dictionary:info")
	public R info(@PathVariable("dictionaryid") Integer dictionaryid){
		DictionaryEntity dictionary = dictionaryService.queryObject(dictionaryid);
		
		return R.ok().put("dictionary", dictionary);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存数据字典")
	@RequestMapping("/save")
	@RequiresPermissions("dictionary:dictionary:save")
	public R save(@RequestBody DictionaryEntity dictionary){
		ValidatorUtils.validateEntity(dictionary, AddGroup.class);
		if (dictionary.getIsreadonly() != null) {
			return R.error("只读属性不可设置");
		}
		dictionary.setCreatetime(new Date());
		dictionary.setUpdatetime(new Date());
		dictionaryService.save(dictionary);
		return R.ok();
	}
	
	/**
	 * 批量修改
	 */
	@SysLog("批量修改数据字典")
	@RequestMapping("/updateBatch")
	@RequiresPermissions("dictionary:dictionary:update")
	public R updateBatch(@RequestBody DictionaryEntity[] dictionarys){
		DictionaryEntity dictionaryEntity = null;
		//修改前判断是否有只读字段
		for(int i= 0;i < dictionarys.length;i++) {
			ValidatorUtils.validateEntity(dictionarys[i], UpdateGroup.class);
			dictionaryEntity = dictionaryService.queryObject(dictionarys[i].getDictionaryid());
			if (ConstantUtils.READONLY.equals(dictionaryEntity.getIsreadonly())) {
				return R.error("只读属性不可修改");
			}
		}
		//循环修改相应字段
		for(int i= 0;i < dictionarys.length;i++) {
			dictionarys[i].setUpdatetime(new Date());
			dictionaryService.update(dictionarys[i]);
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改数据字典")
	@RequestMapping("/update")
	@RequiresPermissions("dictionary:dictionary:update")
	public R update(@RequestBody DictionaryEntity dictionary){
		ValidatorUtils.validateEntity(dictionary, UpdateGroup.class);
		DictionaryEntity dictionaryEntity = dictionaryService.queryObject(dictionary.getDictionaryid());
		if (ConstantUtils.READONLY.equals(dictionaryEntity.getIsreadonly())) {
			return R.error("只读属性不可修改");
		}
		dictionary.setUpdatetime(new Date());
		dictionaryService.update(dictionary);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除数据字典")
	@RequestMapping("/delete")
	@RequiresPermissions("dictionary:dictionary:delete")
	public R delete(@RequestBody Integer[] dictionaryids){
		DictionaryEntity dictionaryEntity = null;
		//删除之前判断是否有只读字段
		for (int i = 0; i < dictionaryids.length;i++){
			dictionaryEntity = dictionaryService.queryObject(dictionaryids[i]);
			if (ConstantUtils.READONLY.equals(dictionaryEntity.getIsreadonly())){
				return R.error("只读字段不可删除");
			}
		}
		dictionaryService.deleteBatch(dictionaryids);
		
		return R.ok();
	}

	@SysLog("多表查询某一类型数据字典列表数据（分页）")
	@RequestMapping("/searchQuery")
	@RequiresPermissions("dictionary:dictionary:list")
	public R searchQuery(@RequestParam Map<String, Object> params){
		//多表查询某一类型列表数据（分页）
		Query query = new Query(params);

		List<DictionaryCustomEntity> dictionaryList = dictionaryService.queryListByTypeName(query);
		int total = dictionaryService.queryByTypeNameTotal(query);

		PageUtils pageUtil = new PageUtils(dictionaryList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	@SysLog("多表查询某一类型数据字典列表数据（不分页")
	@RequestMapping("/searchByTypename")
	@RequiresPermissions("dictionary:dictionary:list")
	public R searchByTypename(String typename){
		//查询某一类型列表数据（不分页）
		if (typename != null && !"".equals(typename.trim())){
			List<DictionaryEntity> dictionaryList = dictionaryService.searchByTypename(typename);
			return R.ok().put("data", dictionaryList);
		}
			return R.error();
	}

	@SysLog("积分配置信息查询")
	@RequestMapping("/queryIntegralConfiguration")
	@RequiresPermissions("dictionary:dictionary:list")
	public R queryIntegralConfiguration(){
		//查询初始积分
		DictionaryEntity integralNumber = dictionaryService.queryObjectByDateTypeName("IntegralNumber");
		DictionaryEntity releaseintegral = dictionaryService.queryObjectByDateTypeName("ReleaseIntegral");
		DictionaryEntity browseintegral = dictionaryService.queryObjectByDateTypeName("BrowseIntegral");
		DictionaryEntity discount = dictionaryService.queryObjectByDateTypeName("Discount");
		DictionaryEntity vipdiscount = dictionaryService.queryObjectByDateTypeName("VIPDiscount");
		Map<String,String> integralConfiguration = new HashMap<>();
		integralConfiguration.put("integralnumber",integralNumber.getDictionaryvalue());
		integralConfiguration.put("releaseintegral",releaseintegral.getDictionaryvalue());
		integralConfiguration.put("browseintegral",browseintegral.getDictionaryvalue());
		integralConfiguration.put("discount",discount.getDictionaryvalue());
		integralConfiguration.put("vipdiscount",vipdiscount.getDictionaryvalue());

		return R.ok().put("data",integralConfiguration);
	}

	
}
